<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="司机手机号" prop="driverPhone">
        <el-input
          v-model="queryParams.driverPhone"
          placeholder="请输入司机手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机姓名" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入司机姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="licensePlate">
        <el-input
          v-model="queryParams.licensePlate"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vehicle:vehicle:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['vehicle:vehicle:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vehicle:vehicle:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['vehicle:vehicle:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vehicleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车辆ID" align="center" prop="vehicleId" />
      <el-table-column label="归属城市" align="center" prop="belongingCity" />
      <el-table-column label="车辆级别" align="center" prop="vehicleLevel" />
      <el-table-column label="选择车型" align="center" prop="vehicleModel" />
      <el-table-column label="司机手机号" align="center" prop="driverPhone" />
      <el-table-column label="司机姓名" align="center" prop="driverName" />
      <el-table-column label="车辆VIN码" align="center" prop="vehicleVin" />
      <el-table-column label="车牌号" align="center" prop="licensePlate" />
      <el-table-column label="是否绑定司机" align="center" prop="hasNavigation">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bind_driver" :value="scope.row.hasNavigation"/>
        </template>
      </el-table-column>
      <el-table-column label="车颜品牌" align="center" prop="vehicleBrand" />
      <el-table-column label="车颜编号" align="center" prop="vehicleColorNumber" />
      <el-table-column label="大屏品牌" align="center" prop="dashboardBrand" />
      <el-table-column label="大屏编号" align="center" prop="dashboardNumber" />
      <el-table-column label="车辆状态" align="center" prop="vehicleStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.vehicle_status" :value="scope.row.vehicleStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="公司标识" align="center" prop="companyIdentifier" />
      <el-table-column label="车辆厂商" align="center" prop="vehicleManufacturer" />
      <el-table-column label="车辆类型" align="center" prop="vehicleType" />
      <el-table-column label="车辆所有人" align="center" prop="vehicleOwner" />
      <el-table-column label="车辆颜色" align="center" prop="vehicleColor" />
      <el-table-column label="发动机号/电动机号" align="center" prop="engineNumber" />
      <el-table-column label="车辆注册日期" align="center" prop="vehicleRegistrationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.vehicleRegistrationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车辆燃料类型" align="center" prop="fuelType" />
      <el-table-column label="发动机排量" align="center" prop="engineDisplacement" />
      <el-table-column label="车辆照片文件编号" align="center" prop="vehiclePhotoFileNumber" />
      <el-table-column label="运输证字号" align="center" prop="transportationPermitNumber" />
      <el-table-column label="车辆运输证发证机构" align="center" prop="vehicleTransportPermitIssuingAuthority" />
      <el-table-column label="车辆经营区域" align="center" prop="vehicleOperationArea" />
      <el-table-column label="车辆运输证有效期起" align="center" prop="vehicleTransportPermitValidFrom" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.vehicleTransportPermitValidFrom, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车辆运输证有效期止" align="center" prop="vehicleTransportPermitValidTo" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.vehicleTransportPermitValidTo, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车辆初次登记日期" align="center" prop="vehicleFirstRegistrationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.vehicleFirstRegistrationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车辆维修状态" align="center" prop="vehicleMaintenanceStatus" />
      <el-table-column label="车辆下次年审时间" align="center" prop="vehicleNextMaintenanceTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.vehicleNextMaintenanceTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车辆年度审核状态" align="center" prop="vehicleAnnualInspectionStatus" />
      <el-table-column label="发票打印设备序列号" align="center" prop="invoicePrintingDeviceNumber" />
      <el-table-column label="卫星定位设备品牌" align="center" prop="satellitePositioningDeviceBrand" />
      <el-table-column label="卫星定位设备型号" align="center" prop="satellitePositioningDeviceModel" />
      <el-table-column label="卫星定位设备MAC号" align="center" prop="satellitePositioningDeviceMac" />
      <el-table-column label="卫星定位设备安装日期" align="center" prop="satellitePositioningDeviceInstallationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.satellitePositioningDeviceInstallationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报备日期" align="center" prop="recordDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recordDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务类型" align="center" prop="serviceType" />
      <el-table-column label="运价类型编码" align="center" prop="fareTypeCode" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['vehicle:vehicle:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vehicle:vehicle:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="归属城市" prop="belongingCity">
          <el-input v-model="form.belongingCity" placeholder="请输入归属城市" />
        </el-form-item>
        <el-form-item label="车辆级别" prop="vehicleLevel">
          <el-input v-model="form.vehicleLevel" placeholder="请输入车辆级别" />
        </el-form-item>
        <el-form-item label="选择车型" prop="vehicleModel">
          <el-input v-model="form.vehicleModel" placeholder="请输入选择车型" />
        </el-form-item>
        <el-form-item label="司机手机号" prop="driverPhone">
          <el-input v-model="form.driverPhone" placeholder="请输入司机手机号" />
        </el-form-item>
        <el-form-item label="司机姓名" prop="driverName">
          <el-input v-model="form.driverName" placeholder="请输入司机姓名" />
        </el-form-item>
        <el-form-item label="车辆VIN码" prop="vehicleVin">
          <el-input v-model="form.vehicleVin" placeholder="请输入车辆VIN码" />
        </el-form-item>
        <el-form-item label="车牌号" prop="licensePlate">
          <el-input v-model="form.licensePlate" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="是否绑定司机" prop="hasNavigation">
          <el-select v-model="form.hasNavigation" placeholder="请选择是否绑定司机">
            <el-option
              v-for="dict in dict.type.bind_driver"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车颜品牌" prop="vehicleBrand">
          <el-input v-model="form.vehicleBrand" placeholder="请输入车颜品牌" />
        </el-form-item>
        <el-form-item label="车颜编号" prop="vehicleColorNumber">
          <el-input v-model="form.vehicleColorNumber" placeholder="请输入车颜编号" />
        </el-form-item>
        <el-form-item label="大屏品牌" prop="dashboardBrand">
          <el-input v-model="form.dashboardBrand" placeholder="请输入大屏品牌" />
        </el-form-item>
        <el-form-item label="大屏编号" prop="dashboardNumber">
          <el-input v-model="form.dashboardNumber" placeholder="请输入大屏编号" />
        </el-form-item>
        <el-form-item label="车辆状态" prop="vehicleStatus">
          <el-select v-model="form.vehicleStatus" placeholder="请选择车辆状态">
            <el-option
              v-for="dict in dict.type.vehicle_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公司标识" prop="companyIdentifier">
          <el-input v-model="form.companyIdentifier" placeholder="请输入公司标识" />
        </el-form-item>
        <el-form-item label="车辆厂商" prop="vehicleManufacturer">
          <el-input v-model="form.vehicleManufacturer" placeholder="请输入车辆厂商" />
        </el-form-item>
        <el-form-item label="车辆所有人" prop="vehicleOwner">
          <el-input v-model="form.vehicleOwner" placeholder="请输入车辆所有人" />
        </el-form-item>
        <el-form-item label="车辆颜色" prop="vehicleColor">
          <el-input v-model="form.vehicleColor" placeholder="请输入车辆颜色" />
        </el-form-item>
        <el-form-item label="发动机号/电动机号" prop="engineNumber">
          <el-input v-model="form.engineNumber" placeholder="请输入发动机号/电动机号" />
        </el-form-item>
        <el-form-item label="车辆注册日期" prop="vehicleRegistrationDate">
          <el-date-picker clearable
            v-model="form.vehicleRegistrationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择车辆注册日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发动机排量" prop="engineDisplacement">
          <el-input v-model="form.engineDisplacement" placeholder="请输入发动机排量" />
        </el-form-item>
        <el-form-item label="车辆照片文件编号" prop="vehiclePhotoFileNumber">
          <el-input v-model="form.vehiclePhotoFileNumber" placeholder="请输入车辆照片文件编号" />
        </el-form-item>
        <el-form-item label="运输证字号" prop="transportationPermitNumber">
          <el-input v-model="form.transportationPermitNumber" placeholder="请输入运输证字号" />
        </el-form-item>
        <el-form-item label="车辆运输证发证机构" prop="vehicleTransportPermitIssuingAuthority">
          <el-input v-model="form.vehicleTransportPermitIssuingAuthority" placeholder="请输入车辆运输证发证机构" />
        </el-form-item>
        <el-form-item label="车辆经营区域" prop="vehicleOperationArea">
          <el-input v-model="form.vehicleOperationArea" placeholder="请输入车辆经营区域" />
        </el-form-item>
        <el-form-item label="车辆运输证有效期起" prop="vehicleTransportPermitValidFrom">
          <el-date-picker clearable
            v-model="form.vehicleTransportPermitValidFrom"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择车辆运输证有效期起">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车辆运输证有效期止" prop="vehicleTransportPermitValidTo">
          <el-date-picker clearable
            v-model="form.vehicleTransportPermitValidTo"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择车辆运输证有效期止">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车辆初次登记日期" prop="vehicleFirstRegistrationDate">
          <el-date-picker clearable
            v-model="form.vehicleFirstRegistrationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择车辆初次登记日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车辆下次年审时间" prop="vehicleNextMaintenanceTime">
          <el-date-picker clearable
            v-model="form.vehicleNextMaintenanceTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择车辆下次年审时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发票打印设备序列号" prop="invoicePrintingDeviceNumber">
          <el-input v-model="form.invoicePrintingDeviceNumber" placeholder="请输入发票打印设备序列号" />
        </el-form-item>
        <el-form-item label="卫星定位设备品牌" prop="satellitePositioningDeviceBrand">
          <el-input v-model="form.satellitePositioningDeviceBrand" placeholder="请输入卫星定位设备品牌" />
        </el-form-item>
        <el-form-item label="卫星定位设备型号" prop="satellitePositioningDeviceModel">
          <el-input v-model="form.satellitePositioningDeviceModel" placeholder="请输入卫星定位设备型号" />
        </el-form-item>
        <el-form-item label="卫星定位设备MAC号" prop="satellitePositioningDeviceMac">
          <el-input v-model="form.satellitePositioningDeviceMac" placeholder="请输入卫星定位设备MAC号" />
        </el-form-item>
        <el-form-item label="卫星定位设备安装日期" prop="satellitePositioningDeviceInstallationDate">
          <el-date-picker clearable
            v-model="form.satellitePositioningDeviceInstallationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择卫星定位设备安装日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报备日期" prop="recordDate">
          <el-date-picker clearable
            v-model="form.recordDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择报备日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="运价类型编码" prop="fareTypeCode">
          <el-input v-model="form.fareTypeCode" placeholder="请输入运价类型编码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVehicle, getVehicle, delVehicle, addVehicle, updateVehicle } from "@/api/vehicle/vehicle"

export default {
  name: "Vehicle",
  dicts: ['bind_driver', 'vehicle_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆列表表格数据
      vehicleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        driverPhone: null,
        driverName: null,
        licensePlate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        belongingCity: [
          { required: true, message: "归属城市不能为空", trigger: "blur" }
        ],
        vehicleLevel: [
          { required: true, message: "车辆级别不能为空", trigger: "blur" }
        ],
        vehicleModel: [
          { required: true, message: "选择车型不能为空", trigger: "blur" }
        ],
        driverPhone: [
          { required: true, message: "司机手机号不能为空", trigger: "blur" }
        ],
        driverName: [
          { required: true, message: "司机姓名不能为空", trigger: "blur" }
        ],
        licensePlate: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        hasNavigation: [
          { required: true, message: "是否绑定司机不能为空", trigger: "change" }
        ],
        vehicleBrand: [
          { required: true, message: "车颜品牌不能为空", trigger: "blur" }
        ],
        vehicleColorNumber: [
          { required: true, message: "车颜编号不能为空", trigger: "blur" }
        ],
        dashboardBrand: [
          { required: true, message: "大屏品牌不能为空", trigger: "blur" }
        ],
        dashboardNumber: [
          { required: true, message: "大屏编号不能为空", trigger: "blur" }
        ],
        vehicleStatus: [
          { required: true, message: "车辆状态不能为空", trigger: "change" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询车辆列表列表 */
    getList() {
      this.loading = true
      listVehicle(this.queryParams).then(response => {
        this.vehicleList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        vehicleId: null,
        belongingCity: null,
        vehicleLevel: null,
        vehicleModel: null,
        driverPhone: null,
        driverName: null,
        vehicleVin: null,
        licensePlate: null,
        hasNavigation: null,
        vehicleBrand: null,
        vehicleColorNumber: null,
        dashboardBrand: null,
        dashboardNumber: null,
        vehicleStatus: null,
        companyIdentifier: null,
        vehicleManufacturer: null,
        vehicleType: null,
        vehicleOwner: null,
        vehicleColor: null,
        engineNumber: null,
        vehicleRegistrationDate: null,
        fuelType: null,
        engineDisplacement: null,
        vehiclePhotoFileNumber: null,
        transportationPermitNumber: null,
        vehicleTransportPermitIssuingAuthority: null,
        vehicleOperationArea: null,
        vehicleTransportPermitValidFrom: null,
        vehicleTransportPermitValidTo: null,
        vehicleFirstRegistrationDate: null,
        vehicleMaintenanceStatus: null,
        vehicleNextMaintenanceTime: null,
        vehicleAnnualInspectionStatus: null,
        invoicePrintingDeviceNumber: null,
        satellitePositioningDeviceBrand: null,
        satellitePositioningDeviceModel: null,
        satellitePositioningDeviceMac: null,
        satellitePositioningDeviceInstallationDate: null,
        recordDate: null,
        serviceType: null,
        fareTypeCode: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.vehicleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加车辆列表"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const vehicleId = row.vehicleId || this.ids
      getVehicle(vehicleId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改车辆列表"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.vehicleId != null) {
            updateVehicle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addVehicle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const vehicleIds = row.vehicleId || this.ids
      this.$modal.confirm('是否确认删除车辆列表编号为"' + vehicleIds + '"的数据项？').then(function() {
        return delVehicle(vehicleIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vehicle/vehicle/export', {
        ...this.queryParams
      }, `vehicle_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
